package com.kaishengit.service;

import java.util.List;

import sun.print.resources.serviceui;

import com.kaishengit.dao.ContactDao;
import com.kaishengit.dao.RecordDao;
import com.kaishengit.entity.Contact;
import com.kaishengit.entity.Record;
import com.kaishengit.entity.User;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.PKutil;
import com.kaishengit.util.Page;

public class ContactService {
	private ContactDao dao = new ContactDao();
	private RecordDao rdao = new RecordDao();
	public Page<Contact> findAllContactByProjectId(String projectid, String p) {
		Page<Contact> page = new Page<Contact>();
		int pageNum = 0;
		if(p == null){
			pageNum = 1;
		}else{
			pageNum = Integer.valueOf(p);
		}
		page.setPageNum(pageNum);
		page.setPageSize(10);
		page.setTotalCount(dao.getAccount(projectid));
		return dao.findAllContactByProjectId(page,projectid);
	}
	public Page<Record> findContactAndRecord(String id,String p) {
		int pageNum =0;
		if(p == null ){
			pageNum = 1;
		}else{
			pageNum = Integer.valueOf(p);
		}
		Page<Record> page = new Page<Record>();
		page.setPageNum(pageNum);
		page.setPageSize(5);
		page.setTotalCount(rdao.getCount(id));
		Page<Record> Page = rdao.findContactAndRecord(id,page);
		return Page;	
	}
	public Contact findContact(String id) {
		return dao.findContact(id);
		
		
	}
	public void saveRecord(User user, String content, String id2) {
		String id = PKutil.getPk();
		String createtime = DateUtil.getNow();
		rdao.saveRecord(user,content,id,createtime,id2);
		
	}
	public void editContact(String company, String contactid, String name,
			String address, String cellphone, String telphone, String email,
			String introduce, String mainpage, String web, String url) {
		Contact contact = new Contact();
		contact.setAddress(address);
		contact.setTelphone(telphone);
		contact.setCompany(company);
		contact.setEmail(email);
		contact.setId(contactid);
		contact.setMainpage(mainpage);
		contact.setIntroduce(introduce);
		if("选择".equals(url)){
			url = "http://sina.com/";
			
			
		}
		String webUrl = url.concat(web);
		contact.setWebUrl(webUrl);
		contact.setCellphone(cellphone);
		contact.setName(name);
		dao.edit(contact);
		
	}
	public void del(String id) {
		rdao.delRecordByContactId(id);
		dao.delByContactId(id);
		
	}
	public void saveContact(String company, String name, String address,
			String cellphone, String telphone, String email, String introduce,
			String mainpage, String web, String url, String projectid) {
		Contact contact = new Contact();
		contact.setAddress(address);
		contact.setTelphone(telphone);
		contact.setCompany(company);
		contact.setEmail(email);
		contact.setId(PKutil.getPk());
		contact.setMainpage(mainpage);
		contact.setIntroduce(introduce);
		if(url.equals("请选择微博")){
			
			
		}else{
			String webUrl = url.concat(web);
			contact.setWebUrl(webUrl);
			
		}
		
		contact.setCellphone(cellphone);
		contact.setName(name);
		contact.setProjectid(projectid);
		dao.save(contact);
		
	}
	
	
}
